package com.xyoto.community.service.impl;

import com.xyoto.community.constant.GuestBookTrimConstant;
import com.xyoto.community.constant.moduleConstant;
import com.xyoto.community.entity.main.GuestBook;
import com.xyoto.community.utils.RedisCommonUtils;
import com.xyoto.community.vo.GuestBookVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class GuestBookRedisImpl {

    @Autowired
    private RedisCommonUtils redisCommonUtils;

    public void saveGuestBook(GuestBookVO guestBookVO) {

        //将留言放入redis中
        redisCommonUtils.ISet(moduleConstant.GUEST_BOOK, guestBookVO);

        //对redis中的留言进行判断大小，大于200条则进行剪切
        if (redisCommonUtils.lGetListSize(moduleConstant.GUEST_BOOK) >= GuestBookTrimConstant.TRIM){
            redisCommonUtils.ltrimList(moduleConstant.GUEST_BOOK,GuestBookTrimConstant.MIN,GuestBookTrimConstant.MAX);
        }
        log.info("留言数量为" + redisCommonUtils.lGetListSize(moduleConstant.GUEST_BOOK));
        log.info("留言板添加到redis后为" + redisCommonUtils.lGet(moduleConstant.GUEST_BOOK,0,-1));
    }
}
